#include "phoneloginwidget.h"

PhoneLoginWidget::PhoneLoginWidget(QWidget *parent)
    : QWidget{parent}
{
    // 设置基本属性
    this->setFixedSize(450, 500);
    this->setAttribute(Qt::WA_DeleteOnClose);
    this->setStyleSheet("QWidget { background-color:rgb(255, 255, 255); }");
    this->setWindowTitle("登录");

    QGridLayout* layout = new QGridLayout();
    layout->setSpacing(10);
    layout->setContentsMargins(50, 50, 50, 0);
    layout->setAlignment(Qt::AlignTop);
    this->setLayout(layout);

    // "登录"
    QLabel* nameLabel = new QLabel("登录");
    nameLabel->setStyleSheet("QLabel { font-size:40px; font-weight:800; "
                             "color:rgb(50, 50, 50); }");

    // 手机号
    QLineEdit* phoneEdit = new QLineEdit();
    phoneEdit->setPlaceholderText("请输入手机号");
    phoneEdit->setFixedHeight(50);
    phoneEdit->setStyleSheet("QLineEdit { font-size:20px; padding-left:10px; "
                              "background-color:rgb(230, 230, 230);"
                              "color:rgb(50, 50, 50); border-radius:10px; "
                              "border:none; }");
   this->setWindowIcon(QIcon(":/resource/image/logo.png"));

    // 验证码
    QLineEdit* verifyCodeEdit = new QLineEdit();
    verifyCodeEdit->setFixedHeight(45);
    verifyCodeEdit->setPlaceholderText("输入验证码");
    verifyCodeEdit->setStyleSheet("QLineEdit { font-size:15px; padding-left:10px; "
                                  "background-color:rgb(230, 230, 230); "
                                  "color:rgb(50, 50, 50); border-radius:10px; "
                                  "border:none; }");

    QPushButton* verifyCodeBtn = new QPushButton("验证码");
    verifyCodeBtn->setFixedHeight(45);
    verifyCodeBtn->setStyleSheet("QPushButton { font-size:25px; color:rgb(50, 50, 50); }"
                                 "QPushButton:hover { background-color:rgb(230, 230, 230); }"
                                 "QPushButton:pressed { background-color:rgb(200, 200, 200); }");

    // 提交按钮
    QPushButton* submitBtn = new QPushButton("提交");
    submitBtn->setFixedHeight(40);
    submitBtn->setStyleSheet("QPushButton { font-size:25px; color:rgb(50, 50, 50); "
                             "background-color:rgb(24, 241, 123); "
                             "border:none; border-radius:10px; }"
                             "QPushButton:hover { background-color:rgb(230, 230, 230); }"
                             "QPushButton:pressed { background-color:rgb(200, 200, 200); }");

    // 用户名登录按钮 + 注册按钮
    QPushButton* userLoginBtn = new QPushButton("用户名登录");
    userLoginBtn->setCursor(Qt::PointingHandCursor);
    userLoginBtn->setFixedHeight(30);
    userLoginBtn->setStyleSheet("QPushButton { font-size:15px; border:none; "
                                  "color:rgb(50, 50, 50); }"
                                  "QPushButton:hover { text-decoration: underline; }");

    QPushButton* switchModeBtn = new QPushButton("注册");
    switchModeBtn->setCursor(Qt::PointingHandCursor);
    switchModeBtn->setFixedHeight(30);
    switchModeBtn->setStyleSheet("QPushButton { font-size:15px; border:none; "
                                 "color:rgb(50, 50, 50); }"
                                 "QPushButton:hover { text-decoration: underline; }");

    // 添加到布局管理器
    layout->addWidget(nameLabel, 0, 1, 1, 3, Qt::AlignCenter);
    layout->addWidget(phoneEdit, 1, 0, 1, 5);
    layout->addWidget(verifyCodeEdit, 2, 0, 1, 3);
    layout->addWidget(verifyCodeBtn, 2, 3, 1, 2);
    layout->addWidget(submitBtn, 3, 1, 1, 3);
    layout->addWidget(userLoginBtn, 4, 0, 1, 1);
    layout->addWidget(switchModeBtn, 4, 4, 1, 1);

    // 点击注册/手机号登录按钮
    connect(userLoginBtn, &QPushButton::clicked, this, [=](){
        // 用户名登录页面
        LoginWidget* login = new LoginWidget(nullptr);
        login->show();
        this->close();
    });

    connect(switchModeBtn, &QPushButton::clicked, this, [=](){
        if(isLoginMode) {
            // 切换到注册模式
            this->setWindowTitle("注册");
            nameLabel->setText("注册");
            switchModeBtn->setText("登录");
        } else {
            // 切换到登录模式
            this->setWindowTitle("登录");
            nameLabel->setText("登录");
            switchModeBtn->setText("注册");
        }
        isLoginMode = !isLoginMode;
    });
}
